package com.yami.shop.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.event.DismissGroupEvent;
import com.yami.shop.bean.model.*;
import com.yami.shop.bean.vo.ReportDetailVO;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ResponseEnum;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.StringUtils;
import com.yami.shop.dao.ReportMapper;
import com.yami.shop.service.*;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements ReportService {
    
    private final ReportMapper reportMapper;
    private final UserService userService;
    private final ShopDetailService shopDetailService;
    private final BlogService blogService;
    private final CommentService commentService;
    private final MeltCloudGroupService meltCloudGroupService;
    private final ApplicationContext applicationContext;

    @Override
    public IPage<ReportDetailVO> page(PageParam<Report> page, ReportDetailVO param) {
        IPage<ReportDetailVO> iPage = reportMapper.page(page, param);
        List<ReportDetailVO> records = iPage.getRecords();
        for (ReportDetailVO record : records) {
            if (!record.getReportSou().equals(2)) {
                if (record.getReportType().equals(0)) {
                    Blog blog = blogService.getById(record.getReportObjId());
                    String s = "";
                    if (blog.getType().equals(0) && !blog.getArticleSource().equals(1)) {
                        s = blog.getTitle();
                    } else {
                        s = blog.getContent();
                    }
                    record.setReportContent(s);
                } else {
                    Comment comment = commentService.getById(record.getReportObjId());
                    record.setReportContent(comment.getContent());
                }
            }
            if (record.getReportType().equals(3)) {
                MeltCloudGroup cloudGroup = meltCloudGroupService.getById(record.getReportObjId());
                record.setReportName(cloudGroup.getGroupName());
                record.setChatStatus(cloudGroup.getIsDismiss());
            } else {
                if (Objects.isNull(record.getReportShopId())) {
                    User user = userService.getUserByUserId(record.getReportUserId());
                    record.setReportName(user.getNickName());
                    if (record.getReportType().equals(2)) {
                        record.setChatStatus(user.getStatus().toString());
                    }
                } else if (record.getReportShopId().equals(0L)) {
                    record.setReportName("平台");
                } else {
                    ShopDetail shop = shopDetailService.getShopDetailByShopId(record.getReportShopId());
                    record.setReportName(shop.getShopName());
                }
            }
        }
        return iPage;
    }

    @Override
    @Transactional
    public void handle(Report report) {
        Report byId = getById(report.getId());
        if (byId.getStatus() != 0) {
            throw new YamiShopBindException(ResponseEnum.SHOW_FAIL, "该条举报已处理！");
        }
        if (StringUtils.isEmpty(report.getOpinion())) {
            throw new YamiShopBindException(ResponseEnum.SHOW_FAIL, "处理意见不能为空！");
        }
        if (report.getStatus().equals(1)) {
            updateById(report);
        } else if (report.getStatus().equals(2)) {
            
            if (byId.getReportSou().equals(2) && byId.getReportType().equals(2)) {
                update(Wrappers.lambdaUpdate(Report.class).set(Report::getStatus, 1).set(Report::getOpinion, report.getOpinion()).set(Report::getHandleUser, report.getHandleUser())
                        .eq(Report::getReportSou, 2).eq(Report::getReportType, 2).eq(Report::getReportUserId, byId.getReportUserId()).eq(Report::getStatus, 0));
            } else {
                update(Wrappers.lambdaUpdate(Report.class).set(Report::getStatus, 1).set(Report::getOpinion, report.getOpinion()).set(Report::getHandleUser, report.getHandleUser())
                        .eq(Report::getReportSou, byId.getReportSou()).eq(Report::getReportType, byId.getReportType()).eq(Report::getReportObjId, byId.getReportObjId()).eq(Report::getStatus, 0));
            }
            // 0：原文 1：评论 2：单聊 3：群聊
            if (byId.getReportType().equals(0)) {
                blogService.update(Wrappers.lambdaUpdate(Blog.class)
                        .set(Blog::getStatus, 2).set(Blog::getReason, report.getOpinion()).eq(Blog::getUid, byId.getReportObjId()));
            } else if (byId.getReportType().equals(1)) {
                commentService.update(Wrappers.lambdaUpdate(Comment.class)
                        .set(Comment::getStatus, 2).set(Comment::getReason, report.getOpinion()).eq(Comment::getUid, byId.getReportObjId()));
            } else if (byId.getReportType().equals(2)) {
                userService.update(Wrappers.lambdaUpdate(User.class)
                        .set(User::getStatus, 0).set(User::getDisableRemark, Constant.REPORT_DISABLE).set(User::getModifyTime, new Date())
                        .eq(User::getUserId, byId.getReportUserId()));
            } else if (byId.getReportType().equals(3)) {
                MeltCloudGroup cloudGroup = meltCloudGroupService.getById(byId.getReportObjId());
                if (cloudGroup.getIsDismiss().equals(0)) {
                    DismissGroupEvent event = DismissGroupEvent.builder().groupId(byId.getReportObjId()).groupName(cloudGroup.getGroupName()).creatorUserId(cloudGroup.getCreatorUserId()).build();
                    applicationContext.publishEvent(event);
                }
            }
        }
    }


}
